define(function(require, exports) {
	var pubjs = require('pubjs');
	var util = require('util');
	var view  = require('@base/view');
	var Detail = require('campaign/modules/creativeDetail').base;

	var Container = view.container.extend({
		init: function(config) {
			config = pubjs.conf(config, {
				 'class': 'M-PageCreativeForm'
				 ,'height': 700
			});

			this.Super('init', arguments);
		}
		,afterBuild: function() {
			this.append([
				'<div class="M-formSection">',
					'<div class="M-formSectionTitle">' + LANG("广告策略") + '</div>',
					'<div class="M-formSectionWraper">',
						'<div class="M-formSectionDesc">',
							'<div class="label">' + LANG("选择本活动关联创意包和落地页。") + '</div>',
							'<div class="list-title">' + LANG("说明：") + '</div>',
							'<ul class="list">',
								'<li>' + LANG("为本活动设定创意包与落地页，可以设定多个创意包和落地页，并可以拖拉关联创意包和落地页。") + '</li>',
								'<li>' + LANG("活动的创意包以及落地页可以各自设定优化策略，默认为智能优化。") + '</li>',
							'</ul>',
						'</div>',
						'<div class="M-PageCreativeFormContainer" />',
					'</div>',
				'</div>'
			].join(''));
		}
		,getContainer: function() {
			return this.find('.M-PageCreativeFormContainer');
		}
	});

	// 电商广告策略
	var Base = Detail.extend({
		init: function(config) {
			config = pubjs.conf(config, {
				'layout': {
					'module': Container
				}
				// 活动类型 1 RTB 2代理 3直投，4移动
				,'channelType': 1
				,'class': 'M-PageCreativeDetail M-PageCreativeDetailListMode'
			});

			this.Super('init', arguments);
		}
		,setData: function(d) {
			var products = d.Products || [];
			var adType = d.AdType;
			if (products.length) {
				var data = products[0];

				//转换成Id成_id
				var tmpData = util.clone(data);
				tmpData['_id'] = tmpData.Id;
				util.each(tmpData.Sweetys, function(val, index){
					val['_id'] = val['Id'];
				});
				util.each(tmpData.WhiskyCreatives, function(val, index){
					val['_id'] = val['Id'];
				});


				// 数据转换，和gameForm接口保持一致, 实际调用creativeDetail的setData
				this.Super('setData',[{
					_id: data._id || 0,
					sweeties: data.Sweetys || [],                         // 关联创意包列表
					sweetyOptimizaType: data.SweetyRatioType || 0,        // 优化方式
					whiskies: data.WhiskyCreatives || [],	              // 关联落地页列表
					whiskyOptimizaType: data.WhiskyCreativeRatioType || 0, // 优化方式
					// 广告监测的广告展示类型
					adType: adType
				}]);
			}
			return this;
		}
		,getData: function() {
			var self = this;
			var data = self.Super('getData', arguments);
			if (!data) {
				return [];
			}

			var result = [{
				// _id: data._id || 0,0
				Id: data._id || 0,
				ServerId: 0,
				RatioSet: 100,
				Sweetys: self.transformList(data.sweeties),
				WhiskyCreatives: self.transformList(data.whiskies),
				SweetyRatioType: data.sweetyOptimizaType,
				WhiskyCreativeRatioType: data.whiskyOptimizaType
			}];
			return result;
		}
		,transformList: function(list){
			var new_list = [];
			util.each(list, function(item){
				var new_item = {
					// _id: item._id,
					Id: item._id,
					RatioSet: item.RatioSet
				};
				if (item.WhiskyIds){
					new_item.WhiskyIds = item.WhiskyIds;
				}
				new_list.push(new_item);
			});
			return new_list;
		}
	});
	exports.base = Base;

	var Main = view.container.extend({
		init: function(config) {
			config = pubjs.conf(config, {
				'class': 'M-PageCreativeFormCon'
				,'channelType': 1
				,'height': 700
				,'view_model': {
					'view': {
						// 广告点击类型
						'LandType': false
						,'AppPackName': false
					}
					,'AppPackName': ''
				 }
			});

			this.Super('init', arguments);
		}
		,afterBuild: function() {
			this.append([
				'<div class="creativeFormCon" pub-name="creativeForm" pub-mod="campaign/modules/creativeForm.base"></div>',
				'<div class="M-formConGroup P-campaignEditProductLabel" ms-visible="view.LandType">',
					'<div class="M-formConGroup">',
						'<label class="group-label">'+LANG('广告点击类型：')+'</label>',
						'<div class="group-content">',
							'<span pub-name="LandType" pub-mod="@base/common/drop.drop"></span>',
						'</div>',
					'</div>',

					'<div class="M-formConGroup" ms-class="dn:!view.AppPackName">',
						'<label class="group-label name">'+LANG('应用包名：')+'</label>',
						'<div class="group-content" >',
							'<input type="text" ms-duplex="AppPackName" class="w40pct">',
							'<span class="ml15" pub-name="AppPackNameTip" pub-mod="@base/tip.desc"/>',
						'</div>',
					'</div>',
				'</div>'
			].join(''));
			var config;
			var c = this.getConfig();
			config = {
				'LandType': {
					'def': LANG('选择类型'),
					'width': 200,
					'options': [
						{'id': 0, 'name': LANG('打开网页')},
						{'id': 1, 'name': LANG('下载ios应用')},
						{'id': 2, 'name': LANG('下载android应用')}
					],
					'panel_config': {
						'hasSearch': false
					}
				},
				'AppPackNameTip': {
					'width': 300,
					'data': LANG('非文件名称，具体内容请联系APP的开发人员')
				}
			};
			this.buildFromTemplate(config);
			this.$isMobile = (c.channelType == 4);
			if(this.$isMobile) {
				this.vm.$.view.LandType = this.$isMobile;
			}
		},
		onDropChange: function(ev) {
			if(ev.name == 'LandType') {
				this.vm.$.view.AppPackName = (+ev.param.id == 2);
			}
		},
		setData: function(data){
			var self = this;
			self.$.creativeForm.setData(data);
			if(self.$isMobile) {
				if(util.isNumber(data.LandType)) {
					self.$.LandType.setValue(data.LandType);
					self.vm.$.view.AppPackName = (data.LandType == 2);
					self.vm.$.AppPackName = data.AppPackName || '';
				}
			}
		},
		getData: function(){
			var result = {
				'Products': this.$.creativeForm.getData() || []
			};
			if(this.$isMobile) {
				result.LandType = this.$.LandType.getValue(true).id;
				if(result.LandType == 2) {
					result.AppPackName = this.vm.getItem('AppPackName') || '';
				}
			}
			return result;
		},
		reset: function(){
			this.$.creativeForm.reset();
			this.$.LandType.reset();
			this.vm.reset();
			this.vm.$.view.LandType = this.$isMobile;
			return this;
		}
	});
	exports.main = Main;
});